/*
 *  $Id: CardRegion.cs 1144 2010-09-30 13:54:07Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008-2009 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System;
using System.Collections.Generic;

using AgateLib.Geometry;
using AgateLib.InputLib;

namespace Ragima.DeckManager {
    public class CardRegion: Region<DeckManagerScene> {
        private bool _reserve;
        private int _num;
        private Card _card;
        private Hint _hint;

        public CardRegion(DeckManagerScene scene, bool reserve, int num, int x, int y): base(new Rectangle(x, y, Constants.SmallCardWidth, Constants.SmallCardHeight), scene) {
            _reserve = reserve;
            _num = num;
            _card = null;
            _hint = null;
        }

        private DeckItem GetCard() {
            if (_num < 0) return null;
            if (_reserve)
                return _num < Scene.Deck.ReserveCount ? Scene.Deck.GetReserveCard(_num) : null;
            else
                return _num < Scene.Deck.Count ? Scene.Deck[_num] : null;
        }

        public override Hint GetHint() {
            UpdateHint();
            return _hint;
        }

        private void UpdateHint() {
            DeckItem item = GetCard();
            Card card = item != null ? item.Card : null;
            if (card != _card) {
                _card = card;
                _hint = card != null ? card.GetHint(Rect) : null;
            }
        }

        public override void Draw() {
            DeckItem card = GetCard();
            if (card == null) return;
            Point p = Rect.Location;
            card.Card.DrawSmall(p.X, p.Y);
            Data.Fonts.CardHealth.Draw(p.X + 4, p.Y + 4, card.Count.ToString());
        }

        public override void OnRightClick() {
            DeckItem card = GetCard();
            if (card == null)
                return;
            if (Keyboard.Keys[KeyCode.Shift])
                Scene.Deck.Remove(card, _reserve);
            else
                Scene.Deck.Remove(card.Card, _reserve);
        }
    }
}
